﻿--[[
@file   Game list module.
@author jameszhai
@date   2017-04-17
--]]

TOfflineModule = 
{
    -- 发行渠道定义
    EDistChnl = 
    {
        dcNormal = 1;
        dcRail   = 2;
    };

    -- 加载模块
    InitModule = function()
        TOfflineModule.zInfoList = {};

        TOfflineModule.zInfoList.count = 0;
        TOfflineModule.zInfoList.zBaseInfo = {};
        TOfflineModule.zInfoList.zInfoMap  = {};

        TOfflineModule.zGameList = {};
    end;

    -- 卸载模块
    UnInitModule =function()
        TOfflineModule.ResizeGameList(0);
        TOfflineModule.zGameList = {};
    end;

    SetSelfInfo = function(zParam)
        TOfflineModule.zSelfInfo = zParam;
    end;

    GetMyGameList = function()
        return TOfflineModule.zGameList;
    end;

    -- 获取我的游戏列表
    GetGameInfoList = function()
        return TOfflineModule.zInfoList;
    end;

    -- 获取游戏信息
    GetGameInfo = function(nGameID)
        if (nil ~= nGameID) and (nil ~= TOfflineModule.zInfoList) and 
           (nil ~= TOfflineModule.zInfoList.zInfoMap) then
            return TOfflineModule.zInfoList.zInfoMap[nGameID];
        else
            return nil;
        end;
    end;

    -- 更新我的游戏列表
    UpdateMyGameList = function()
        local pGMLib = base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");

        if pGMLib ~= nil then
            TOfflineModule.zGameList = {};

            local zList = ierd_tgp.game_library.Game_list:new();
            pGMLib:get_visible_games(zList);

            for nIdx = 0,  zList.count - 1 do
                table.insert(TOfflineModule.zGameList, 
                             zList.game_id_list[nIdx]);
            end;

            zList:delete();

            -- 更新所有游戏状态
            if nil == TOfflineModule.zStateMap or #TOfflineModule.zStateMap <= 0 then
                TOfflineModule.UpdateGameState(0);
            end;

            -- 更新游戏信息
            if nil == TOfflineModule.zInfoList or TOfflineModule.zInfoList.count <= 0 then
                TOfflineModule.UpdateGameInfo();
            end;
        end;
    end;

    -- 是否为单机游戏
    IsRailGame = function(nGameID)
        if nil == nGameID or nGameID <= 0 then
            return false;
        end;

        local pGMLib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");

        if nil == pGMLib then
            TGPLog.LogE("OfflineM", "IsRail game, game lib is nil");
            return false;
        end;

        local nChnl = pGMLib:get_game_distribution_channels(nGameID);

        if nChnl == TOfflineModule.EDistChnl.dcRail then
            return true;
        else
            return false;
        end
    end;

    -- 更新游戏状态
    UpdateGameState = function(nGameID)
        if 0 == nGameID then
            if nil == TOfflineModule.zGameList or #TOfflineModule.zGameList <= 0 then
                TGPLog.LogE("OfflineM", "Update state, my game list is empty.");
                return false;
            end;

            if nil == TOfflineModule.zStateMap then
                TOfflineModule.zStateMap = {};
            end;

            -- 获取所有游戏的状态
            for nIdx = 1, #TOfflineModule.zGameList do
                TOfflineModule.GetGameState(TOfflineModule.zGameList[nIdx]);
            end;

            return true;
        else
            if nil ~= TOfflineModule.GetGameState(nGameID) then
                return true;
            else
                return false;
            end;
        end;
    end;

    -- 获取所有游戏状态
    GetAllState = function()
        return TOfflineModule.zStateMap;
    end;

    -- 根据游戏ID获取游戏状态
    GetGameState = function(dwGameID)
        local pGMLib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");

        if nil == pGMLib then
            TGPLog.LogE("OfflineM", "Update game info, game lib is nil");
            return nil;
        end;

        if nil == TOfflineModule.zStateMap then
            TOfflineModule.zStateMap = {};
        end;

        local   zState = nil;

        if nil ~= TOfflineModule.zStateMap[dwGameID] then
            zState = TOfflineModule.zStateMap[dwGameID];
        else
            zState = ierd_tgp.game_library.GameState:new();
            TOfflineModule.zStateMap[dwGameID] = zState;
        end;

        pGMLib:get_game_state(dwGameID, zState);
        return zState;
    end;

    -- 更新所有游戏
    UpdateGameInfo = function()
        local pGMLib = base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");

        if nil == pGMLib then
            TGPLog.LogE("OfflineM", "Update game info, game lib is nil");
            return;
        end;

        local zList = ierd_tgp.game_library.GameBaseInfoIDList:new();
        if nil == zList then
            banner_control.inner_log("[update_all_game_base_info]GameBaseInfoIDList:new() fail");
            return;
        end;
        
        pGMLib:GetAllGameBaseInfoIDList(zList);

        TOfflineModule.ResizeGameList(zList.count);

        for nIdx = 0, (zList.count - 1) do
            local nGameID = zList.game_id_list[nIdx];

            if nil == TOfflineModule.zInfoList.zBaseInfo[nIdx] then
                TOfflineModule.zInfoList.zBaseInfo[nIdx] = 
                    ierd_tgp.game_library.GameBaseInfo:new();
            end;

            pGMLib:GetGameBaseInfo(nGameID, TOfflineModule.zInfoList.zBaseInfo[nIdx]);
            TOfflineModule.zInfoList.zInfoMap[nGameID] = TOfflineModule.zInfoList.zBaseInfo[nIdx];
        end;

        zList:delete();
    end;

    -- 修改游戏列表大小
    ResizeGameList = function(nSize)
        if nSize < TOfflineModule.zInfoList.count then
            for nIdx = nSize, TOfflineModule.zInfoList.count - 1 do
                local zTemp = TOfflineModule.zInfoList.zBaseInfo[nIdx];

                TOfflineModule.zInfoList.zInfoMap[zTemp.id] = nil;
                TOfflineModule.zInfoList.zBaseInfo[nIdx]:delete();
                TOfflineModule.zInfoList.zBaseInfo[nIdx] = nil;
            end;
        end;

        TOfflineModule.zInfoList.count = nSize;
    end;

    update_single_game_base_info = function(param_tbl)
        local game_id = 0;
        if param_tbl ~= nil and param_tbl.game_id ~= nil then
            game_id = param_tbl.game_id;
        end
        if game_id == 0 then return end
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil == game_lib then
            banner_control.inner_log("[update_single_game_base_info]game_lib is nil");
            return;
        end;

        local find_it = false;
        for i = 0, (TOfflineModule.zInfoList.count - 1) do
            if TOfflineModule.zInfoList.zBaseInfo[i].id == game_id then
                game_lib:GetGameBaseInfo(game_id, TOfflineModule.zInfoList.zBaseInfo[i]);
                TOfflineModule.zInfoList.zInfoMap[game_id] = TOfflineModule.zInfoList.zBaseInfo[i];
                find_it = true;
                break;
            end;
        end;
        if not find_it then
            local count = TOfflineModule.zInfoList.count;
            TOfflineModule.zInfoList.zBaseInfo[count] = 
                    ierd_tgp.game_library.GameBaseInfo:new();
            game_lib:GetGameBaseInfo(game_id,
                TOfflineModule.zInfoList.zBaseInfo[count]);
            TOfflineModule.zInfoList.zInfoMap[game_id] = TOfflineModule.zInfoList.zBaseInfo[count];
            TOfflineModule.zInfoList.count = count + 1;
        end
    end;

   update_batch_game_base_info = function(param_tbl)
        TGPLog.LogE("OfflineM", "update_batch_game_base_info");
        if param_tbl == nil or param_tbl.game_idList == nil or
            type(param_tbl.game_idList) ~= 'table' then
            return
        end
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil == game_lib then
            TGPLog.LogE("OfflineM", "[update_batch_game_base_info]game_lib is nil");
            return;
        end;

        for index, game_id in ipairs(param_tbl.game_idList) do
            local find_it = false;
            for i = 0, (TOfflineModule.zInfoList.count - 1) do
                if TOfflineModule.zInfoList.zBaseInfo[i].id == game_id then
                    find_it = true;
                    break;
                end;
            end;
            if not find_it then
                local count = TOfflineModule.zInfoList.count;
                TOfflineModule.zInfoList.zBaseInfo[count] = 
                        ierd_tgp.game_library.GameBaseInfo:new();
                game_lib:GetGameBaseInfo(game_id,
                    TOfflineModule.zInfoList.zBaseInfo[count]);
                TOfflineModule.zInfoList.zInfoMap[game_id] = TOfflineModule.zInfoList.zBaseInfo[count];
                TOfflineModule.zInfoList.count = count + 1;
            end
        end
    end;

    zInfoList = nil;    -- 游戏基本信息列表
    zGameList = nil;    -- 我的游戏列表
    zStateMap = nil;    -- 游戏状态
    zSelfInfo = nil;    -- 自己的信息

};